const {
    ordersModel
} = require('./models/ordersModel');
const {
    tenantsModel
} = require('./models/tenantsModel');
const {
    parkingsModel
} = require('./models/parkingsModel');

//获取订单
module.exports.getOrders = async function (params) {
    // console.log("获取订单的params", params);
    const {
        searchBy,
        searchValue,
        currentPage,
        pageSize
    } = params;
    const date = searchValue.substr(0, 10);
    // console.log('下单日期searchValue', date);
    const data = await ordersModel.find({
            delete: false,
        })
        .populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    const total = await ordersModel.find({ //total:返回的数据总数
            delete: false,
        })
        .populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .countDocuments(true);
    return {
        data,
        total
    }
}
//软删除订单
module.exports.deleteOrders = async function (data) { //data 的值为 {_id:...}
    const msg = await ordersModel.updateOne(data, {
        delete: true
    });
    // const item = await ordersModel.find(data);
    // console.log('修改了delete属性：', item);
    return msg;
}

//修改
module.exports.modifyOrders = async function (data) {
    const {
        _id,
        state
    } = data;

    const msg = await ordersModel.updateOne({
        '_id': _id
    }, {
        'state': state
    });

    return msg;
}
//分类查询
module.exports.getOrdersByState = async function (params) {
    const {
        searchBy,
        searchValue,
        currentPage,
        pageSize
    } = params;
    const data = await ordersModel.find({
            delete: false,
            state: searchValue
        }).populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    const total = await ordersModel.find({
            delete: false,
            state: searchValue
        })
        .populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .countDocuments(true);
    return {
        data,
        total
    }
}
//按顾客姓名查找。使用顾客数据模型查找
module.exports.getOrdersByTenantsName = async function (params) {
    const {
        searchValue,
        currentPage,
        pageSize
    } = params;
    let tenant = await tenantsModel
        .find({
            "name": {
                $regex: searchValue,
                $options: '$i'
            }
        })
        .populate({
            path: 'ordersId',
            populate: {
                path: 'parkingsId tenantsId',
                populate: {
                    path: 'lessorsId'
                }
            },
        });
    //tenant =>顾客对象，ordersId => 订单数组//默认所有用户名字完全不同
    //slice(开始下标，结束下标) 实现翻页
    let data = tenant[0].ordersId.filter(item => !item.delete)
        .slice((currentPage - 1) * pageSize, (currentPage - 1) * pageSize + pageSize);
    //该用户关联的未删除的订单数量
    let total = tenant[0].ordersId.filter(item => !item.delete).length;
    return {
        data,
        total,
    }
}

//按车位地址查找
module.exports.getOrdersByParking = async function (params) {
    const {
        searchBy,
        searchValue,
        currentPage,
        pageSize
    } = params;
    let parking = await parkingsModel
        .find({
            "address": {
                $regex: searchValue,
                $options: '$i'
            }
        }).populate({
            path: 'ordersId',
            populate: {
                path: "parkingsId tenantsId",
                populate: {
                    path: 'lessorsId'
                }
            }
        });
    let data = parking[0].ordersId.filter(item => !item.delete)
        .slice((currentPage - 1) * pageSize, (currentPage - 1) * pageSize + pageSize);
    let total = parking[0].ordersId.filter(item => item.delete).length;
    return {
        data,
        total
    }

}


//按订单编号查
var ObjectId = require('mongodb').ObjectId;
module.exports.getOrdersById = async function (params) {
    const {
        searchBy,
        searchValue,
        currentPage,
        pageSize
    } = params;
    let data = await ordersModel
        .find({
            'delete': false,
            '_id': ObjectId(searchValue) //____________
        })
        .populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    console.log('----------id查询data', data);
    const total = await ordersModel
        .find({
            'delete': false,
            '_id': ObjectId(searchValue)
        })
        .populate('tenantsId').populate({
            path: 'parkingsId',
            populate: {
                path: 'lessorsId'
            },
        })
        .countDocuments(true);
    console.log('----------id查询total', total);
    return {
        data,
        total
    }
}

//新增评价内容addEvaluate

module.exports.addEvaluate = async function (data) { //data 的值为 {_id:...}
    console.log('持久层评价', data);
    const msg = await ordersModel.updateOne({
        _id: data._id
    }, {
        evaluateStar: data.evaluateStar,
        evaluateContent: data.myEvaluateContent,
        isEvaluate: true
    });
    // const item = await ordersModel.find(data);
    // console.log('修改了delete属性：', item);
    // console.log('修改回来', msg);
    return msg;
}

//查询历史订单
module.exports.getHistoryOrders = async function (val) {
    console.log(val);
    const totalNum = (await ordersModel.find({
        [val.options]: {
            $regex: val.search_data,
            $options: '$i'
        },
        delete: false,
        state: 4,
        // lessorsid: "5fad03e8842d3427f8660183"
    })).length;
    //如果val是一个空对象的话，依然是搜索所有模块。
    const data = await ordersModel.find({
            [val.options]: {
                $regex: val.search_data,
                $options: '$i'
            },
            delete: false,
            state: 4,
        })
        .populate('tenantsId').populate('parkingsId')
        .limit(val.pageSize - 0)
        .skip((val.currentPage - 1) * val.pageSize);
    const parkingObj = {
        data: [],
        totalPage: 0,
        totalNum: 0,
        currentPage: val.currentPage - 0
    }
    // console.log('持久层搜回的数据----------------', data);

    parkingObj.data = data;
    parkingObj.totalPage = Math.ceil(totalNum / val.pageSize); //获取总共有多少页
    parkingObj.totalNum = totalNum;
    console.log('持久层搜回的数据----------------', parkingObj);
    return parkingObj;

}